#!/bin/sh

# Slackware build script for evince

# Copyright 2009  Andrew Brouwers <abrouwers@gmail.com>
# Copyright 2009-2014, Michiel van Wessem, Manchester, United Kingdom.
# Copyright 2019, B. Watson
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Now maintained by B. Watson <yalhcru@gmail.com>

# 20191231 bkw:
# - take over maintenance
# - update for v3.28.5.3, which is the last version that will build with
#   Slack 14.2's rather outdated gtk+3 libraries. Actually it requires
#   some fugly hackery to work, see shim.h for gory details and polemic.

PRGNAM=evince
VERSION=${VERSION:-3.28.5}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.xz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# If we have any patches apply them here:
#  http://sources.gentoo.org/cgi-bin/viewvc.cgi/gentoo-x86/app-text/evince/files/

# 20191231 bkw: I'll leave this here, but I have NOT tested it.
# It was never documented in README, caveat hactor.
NLS=${NLS:-YES}
if [ "$NLS" != "YES" ]; then
    # disable NLS - we're going for a small package here.
    rm po/*.po
    sed -i -e 's/USE_NLS=yes/USE_NLS=no/g' \
      -e 's/ENABLE_NLS 1/ENABLE_NLS 0/g' configure
fi

# 20200101 bkw: "shim" to let us actually compile with gtk+-3.18 and
# glib-2.46.2. Supposedly these versions are supported by evince-3.28.5,
# but upstream uses a couple of functions that are new in gtk+-3.22
# and glib-2.52. Fortunately it's stuff we can work around with a few
# #defines.
SLKCFLAGS="$SLKCFLAGS -include $CWD/shim.h"

# 20200101 bkw: 14.2's mime stuff identifies djvu files as
# image/vnd.djvu only, but evince wants image/vnd.djvu+multipage.
sed -i '/^ *DJVU_MIME_TYPES/s|="|&image/vnd.djvu;|' configure

# If we were building 3.30.x, we'd need this:
# --without-gspell \

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --mandir=/usr/man \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --enable-dbus \
  --enable-pdf \
  --enable-tiff \
  --enable-djvu \
  --enable-t1lib \
  --enable-comics \
  --enable-dvi \
  --enable-ps \
  --enable-xps \
  --enable-introspection \
  --disable-static \
  --disable-nautilus \
  --disable-maintainer-mode \
  --with-systemduserunitdir=no \
  --build=$ARCH-slackware-linux

# ugh. glib-genmarshal's flags changed gratuitously.
sed -i 's,--prototypes,--header,' libview/Makefile

make V=1
make install-strip DESTDIR=$PKG

# Fix desktop entry
sed -i "/NoDisplay=true/d" $PKG/usr/share/applications/evince.desktop

gzip -9 $PKG/usr/man/man1/$PRGNAM.1

rm -rf $PKG/{usr/share/gtk-doc,etc}
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a AUTHORS COPYING NEWS README MAINTAINERS $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
